home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / bash-112.lha / bash-1.12 / newversion.c < prev    next >
C/C++ Source or Header  |  1991-07-07  |  4KB  |  172 lines

  1. /* Simple program to make new version numbers for the shell.
  2.    Big deal, but it was getting out of hand to do everything
  3.    in the makefile. */
  4.  
  5. /* Copyright (C) 1989 Free Software Foundation, Inc.
  6.  
  7. This file is part of GNU Bash, the Bourne Again SHell.
  8.  
  9. Bash is free software; you can redistribute it and/or modify it under
  10. the terms of the GNU General Public License as published by the Free
  11. Software Foundation; either version 1, or (at your option) any later
  12. version.
  13.  
  14. Bash is distributed in the hope that it will be useful, but WITHOUT ANY
  15. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  17. for more details.
  18.  
  19. You should have received a copy of the GNU General Public License along
  20. with Bash; see the file COPYING.  If not, write to the Free Software
  21. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  22.  
  23. #include <stdio.h>
  24. char *progname;
  25.  
  26. main (argc, argv)
  27.      int argc;
  28.      char **argv;
  29. {
  30.   FILE *file, *must_open ();
  31.   float distver = 0.0;
  32.   int dist = 0, build = 0, buildver = 0;
  33.   int dist_inc = 0, build_inc = 0;
  34.   int dot_dist_needs_making = 0;
  35.   int arg_index = 1;
  36.  
  37.   progname = argv[0];
  38.  
  39.   while (arg_index < argc && argv[arg_index][0] == '-')
  40.     {
  41.       if (strcmp (&(argv[arg_index][1]), "dist") == 0)
  42.     dist++;
  43.       else if (strcmp (&(argv[arg_index][1]), "build") == 0)
  44.     build++;
  45.       else
  46.     {
  47.       fprintf (stderr, "Bad flag to %s: expected -dist or -build.\n",
  48.            argv[0]);
  49.       exit (1);
  50.     }
  51.       arg_index++;
  52.     }
  53.  
  54.   if (!get_float_from_file (".distribution", &distver))
  55.     dot_dist_needs_making++;
  56.  
  57.   if (!dist)
  58.     {
  59.       get_int_from_file (".build", &buildver);
  60.       build_inc++;
  61.     }
  62.   else
  63.     dist_inc++;
  64.  
  65.   if (dist && arg_index < argc)
  66.     if (1 != sscanf (argv[arg_index], "%f", &distver))
  67.       {
  68.     fprintf (stderr, "Bad input `%s'.  Expected float value for -dist.\n",
  69.          argv[arg_index]);
  70.     exit (1);
  71.       }
  72.     else
  73.       {
  74.     arg_index++;
  75.     dist_inc = 0;
  76.       }
  77.  
  78.   if (build && arg_index < argc)
  79.     if (1 != sscanf (argv[arg_index], "%d", &buildver))
  80.       {
  81.     fprintf (stderr, "Bad input `%s'.  Expected int value for -build.\n",
  82.          argv[arg_index]);
  83.     exit (1);
  84.       }
  85.     else
  86.       {
  87.     arg_index++;
  88.     build_inc = 0;
  89.       }
  90.  
  91.   if (dot_dist_needs_making && !distver)
  92.     {
  93.       fprintf (stderr, "There is no `.distribution' file for me to infer from.\n");
  94.       exit (1);
  95.     }
  96.  
  97.   if (build)
  98.     buildver = buildver + 1;
  99.   
  100.   if (dist_inc)
  101.     distver = distver + 0.01;
  102.  
  103.   file = must_open ("newversion.h", "w");
  104.   fprintf (file, "%s%.2f%s%d\n","\
  105. /* Version control for the shell.  This file gets changed when you say\n\
  106.    `make newversion' to the Makefile.  It is created by newversion.aux. */\n\
  107. \n\
  108. /* The distribution version number of this shell. */\n\
  109. #define DISTVERSION \"", distver, "\"\n\n/* The last built version of this shell. */\n\
  110. #define BUILDVERSION ", buildver);
  111.   fclose (file);
  112.  
  113.   file = must_open (".build", "w");
  114.   fprintf (file, "%d\n", buildver);
  115.   fclose (file);
  116.  
  117.   if (dist)
  118.     {
  119.       file = must_open (".distribution", "w");
  120.       fprintf (file, "%.2f\n", distver);
  121.       fclose (file);
  122.     }
  123.   exit (0);
  124. }
  125.  
  126. get_float_from_file (filename, var)
  127.      char *filename;
  128.      float *var;
  129. {
  130.   FILE *stream;
  131.   int result;
  132.  
  133.   if ((stream  = fopen (filename, "r")) == (FILE *)NULL)
  134.     return (1);
  135.   result = fscanf (stream, "%f\n", var);
  136.   fclose (stream);
  137.   return (result == 1);
  138. }
  139.  
  140. get_int_from_file (filename, var)
  141.      char *filename;
  142.      int *var;
  143. {
  144.   FILE *stream;
  145.   int result;
  146.  
  147.   if ((stream  = fopen (filename, "r")) == (FILE *)NULL)
  148.     return (1);
  149.   result = fscanf (stream, "%d\n", var);
  150.   fclose (stream);
  151.   return (result == 1);
  152. }
  153.  
  154. FILE *
  155. must_open (name, mode)
  156.      char *name, *mode;
  157. {
  158.   FILE *temp = fopen (name, mode);
  159.  
  160.   if (!temp)
  161.     {
  162.       fprintf (stderr, "%s: Cannot open `%s' for mode `%s'.\n",
  163.            progname, name, mode);
  164.       fprintf
  165.     (stderr,
  166.      "Perhaps you don't have %s permission to the file or directory.\n",
  167.      (strcmp (mode, "w") == 0) ? "write" : "read");
  168.       exit (3);
  169.     }
  170.   return (temp);
  171. }
  172.